import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/event_bus.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/login&register/login_textfield.dart';
import 'package:find_friend/pages/login&register/model/shared_user_info.dart';
import 'package:flutter/material.dart';

class MineModNamePage extends StatefulWidget {
  @override
  _MineModNamePageState createState() => _MineModNamePageState();
}

class _MineModNamePageState extends State<MineModNamePage> {

  TextEditingController _controller = TextEditingController();
  int maxlength = 10;
  FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _initData();
  }

  _initData() async {
    SharedUserInfo user = await SharedUserInfo.instance();
    _controller.text = user.nickname;
    maxlength = 10 - user.nickname.length <= 0 ? 0 : 10 - user.nickname.length;
    if(mounted) setState(() {});
  }

  _request() {
    Networktool.request(API.edit, params: {"nickname": _controller.text}, success: (data){
      print(data);
      if(data["data"] != null) {
        SharedUserInfo.saveData(data, isupdateIM: true);
        AlertTool.showToast("修改成功");
        EventMineBus.instance.commit(EventMineKeys.UpdateMineInfo, null);
        Navigator.of(context).pop(true);
      }
    },
    fail: (msg) => AlertTool.showToast(msg));
  }

  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text("修改我的昵称"),
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png")
        ),
        actions: [
          FlatButton(
            onPressed: (){
              if(_controller.text == "") {
                AlertTool.showToast("昵称不能为空");
                return;
              }
              _request();
            },
            child: Text("完成", style: instanceTextStyle(14, APP_font_black,fontWeight: FontWeight.normal)),
          )
        ],
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: (){
          focusNode.unfocus();
        },
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 37.5),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              LoginTextField(
                marginTop: 10,
                focusNode: focusNode,
                controller: _controller,
                placeholder: "请输入昵称",
                maxlength: 10,
                callback: (str){
                  print("$str");
                  setState(() {
                    maxlength = 10 - str.length <= 0 ? 0 : 10 - str.length;
                  });
                },
              ),
              Container(
                padding: const EdgeInsets.only(top: 5, right: 10),
                child: Text("$maxlength",style: instanceTextStyle(11, APP_font_grey))),
              Spacer(),
              GestureDetector(
                  onTap: _request,
                  child: Container(
                    width: size.width,
                    height: 40,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black, width: 1.5),
                      borderRadius: BorderRadius.circular(20)
                    ),
                    margin: const EdgeInsets.symmetric(horizontal: 62, vertical: 50),
                    child: Text("保存", style: instanceTextStyle(18, APP_font_black),),
                  ),
                )
            ],
          )
        ),
      )
    );
  }
}